package com.it.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyDateUtil {
    //string转换成date     String strDate = sc.next();
    public static Date strToDate(String strDate) {

//        String inputDate = "2020/11/12 19:17:16"; 标准格式

        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date deadDate = null;
        try {
            deadDate = dateFormat1.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return deadDate;
    }

    //date转换成string   yyyy-MM-dd HH:mm:ss
    public static String dateToStr(Date date1) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date1);
        return format;
    }

    //返回两日期之间间隔（相隔）多少天
    public static long returnInterval(Date d1Small, Date d2Big) {
        long specialDate = d1Small.getTime();
        long specialDate2 = d2Big.getTime();
        long betweenDate = (specialDate2 - specialDate) / (1000 * 60 * 60 * 24);
        return betweenDate;
    }

    //返回两日期之间间隔（相隔）多少小时
    public static long returnHours(Date d1Small, Date d2Big) {
        long specialDate = d1Small.getTime();
        long specialDate2 = d2Big.getTime();
        long betweenDate = (specialDate2 - specialDate) / (1000 * 60 * 60);
        return betweenDate;
    }
}
